package com.project.service.sys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.common.TreeNode;
import com.project.service.BaseService;
import com.project.dao.sys.MenuDao;
import com.project.entity.sys.SysMenu;
import com.project.util.JacksonUtil;

@Transactional
@Service
public class MenuService extends BaseService<SysMenu>{

	@Autowired
	private MenuDao menuDao;
	
	public List<SysMenu> getFirstMenu(){
		SysMenu menu = new SysMenu();
		menu.setcOrder(Order.asc("sort"));
		menu.setParentId(0L);
		return menuDao.query(menu);
	}

	public String queryList() throws IOException {
		SysMenu sysMenu = new SysMenu();
		sysMenu.setcOrder(Order.asc("sort"));
		List<SysMenu> list = menuDao.query(sysMenu);
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		
		for(SysMenu menu:list){
			TreeNode treeNode = new TreeNode();
			treeNode.setId(String.valueOf(menu.getId()));
			treeNode.setpId(menu.getParentId()==null ? "":String.valueOf(menu.getParentId()));
			treeNode.setName(menu.getName());
			treeNode.setChecked(false);
			treeNode.setOpen(true);
			treeNode.setChkDisabled(false);
//			treeNode.setCode(entity.getCode());
//			treeNode.setDepartmentCode(entity.getDepartmentCode());
			
			treeNodeList.add(treeNode);
		}
		
		return JacksonUtil.objToJson(treeNodeList);
	}

	public void update(SysMenu entity) {
		// TODO Auto-generated method stub
		menuDao.update(entity);
	}
}
